package com.itheima.Controller;

import com.itheima.utils.AliOSSProperties;
import com.itheima.entity.Result;
import com.itheima.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/*
@Slf4j
@RestController
public class UploadController {
    //文件上传本地存储
    @PostMapping("/upload")
    public Result upload(String username, Integer age , MultipartFile file) throws IOException {
        log.info("{},{},{}",username,age,file);
        //1.获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //2.通过UUID生成随机字符串
        String newFileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));//获取文件后缀
        //2.需要将前端上传的文件存到本地
        file.transferTo(new File("D:\\"+newFileName));
        return Result.success();
    }
}
*/
@Slf4j
@RestController
public class UploadController {
    //文件上传阿里云oss存储
   // private final String bucketName="ranner";
   // private final String endpoint="https://oss-cn-shenzhen.aliyuncs.com";

  /*  @Value("${aliyun.oss.bucket}")
    private  String bucketName;
    @Value("${aliyun.oss.endpoint}")
    private  String endpoint;*/
    @Autowired
    private AliOSSProperties aliOSSProperties;
    @PostMapping("/upload")
    public Result upload( MultipartFile file) throws Exception {
        log.info("{}",file);
        //1.获取原始文件名截取后缀
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));//获取文件后缀

        //2.调用阿里云工具类,将文件上传到oss
        String url = AliyunOSSUtils.upload(aliOSSProperties.getEndpoint(),aliOSSProperties.getBucket(), file.getBytes(), extName);

        //3.返回图片路径
        return Result.success(url);
    }
}

